package com.dyg.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dyg.model.Resource;
import com.dyg.service.ResourceService;


@Controller
@RequestMapping("/admin/resource")
public class ResourceController {

	@Autowired
	private ResourceService resourceService;
	
	
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public String list(Model model){
		List<Resource> findResourceList = resourceService.findResourceList();
		model.addAttribute("resourceList",findResourceList);
		return "resource/list";
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String add(Model model){
		
		return "resource/add";
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String add(
			HttpServletRequest request,
			String name,
			String permission,
			String url){
		
		Resource resource = new Resource();
		resource.setName(name);
		resource.setPermission(permission);
		resource.setUrl(url);
		Resource insertResource = resourceService.insertResource(resource);
		
		return "redirect:list";
	}
	
	@RequestMapping(value = "/{id}",method = RequestMethod.GET)
	public String update(@PathVariable("id")Integer id,Model model){
		Resource resource = resourceService.findResourceById(id+"");
		model.addAttribute("resource",resource);
		
		return "resource/update";
	}
	
	@RequestMapping(value = "/{id}",method = RequestMethod.POST)
	public String update(@PathVariable("id")Integer id,Resource resource){
		resource.setId(id);
		Resource updateResource = resourceService.updateResource(resource);
		return "redirect:list";
	}
}
